package algorithm.binaryTree;

import java.util.Stack;

public class tsSumOfLeftLeaves {
    public static void main(String[] args) {

        TreeNode node15 = new TreeNode(15);
        TreeNode node7 = new TreeNode(7);
        TreeNode node20 = new TreeNode(20, node15, node7);

        TreeNode node9 = new TreeNode(9);
        TreeNode root = new TreeNode(3 , node9, node20);

        Calulate calulate = new Calulate();
        int b = calulate.sumOfLeftLeaves(root);
        System.out.println(b);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static class Calulate{ //放入 class Solution中的方法来测试：

        public int sumOfLeftLeaves(TreeNode root) {

            //方法1：递归遍历  //1：参数与返回值
            return sumLeft(root);
        }

        private int sumLeft(TreeNode root){
            //2终止条件：左叶子空/叶子空
            // 3 单层处理 :是左叶子呀？
            if(root == null ) return 0;
            int sum =0;
            if(root.left != null && root.left.left== null && root.left.right == null)
        // 判断当前节点是不是左叶子是无法判断的，必须要通父节点来判断：
            //找到了一个左叶子：如果【该节点的左节点】不为空，该节点的左节点的左节点为空，该节点的左节点的右节点为空
                sum += root.left.val;//该节点的左节点是左叶子

//            return sum+sumLeft(root, root.left) +  sumLeft(node, node.right);


            int sum1= sumLeft(root.left);
            int sum2=  sumLeft(root.right);

            return sum + sum1 +  sum2;

        }




    }

}
